<?php
class CustomerModel extends model
{
	static $_instance = null;

	static function getInstance ()
	{
		if (is_null(self::$_instance)) {
			self::$_instance = new self();
		}
		return self::$_instance;
	}
	
	// 顾客(总件数)
	function getCount($p){
		$sqltemp  = "select cc.id as cid,su.id as sid,su.user as users,if(su.stop=1,'停用','启用') as stopname,'密' as pwd,ca.name as area,cc.* ";
		$sqltemp .= "from sys_user as su,crm_customer as cc left join crm_area as ca on cc.area_id = ca.id ";
		$sqltemp .= "where su.ugroup = 1 and cc.login = su.id  ";
		$sql  = "select count(*) as c from ($sqltemp)a where 1=1 ";
		if($p["user"] != ""){$sql .= " and users like '%".$p["user"]."%'";}
		if($p["name"] != ""){$sql .= " and name like '%".$p["name"]."%'";}
		if($p["area"] != ""){$sql .= " and area_id in (".$p["area"].")";}
		if($p["types"] != ""){$sql .= " and types = '".$p["types"]."'";}

		$return = $this->sql($sql)->s(1);
		return $return["c"];
	}
	
	// 顾客(列表)
	function getCustomer($p)
	{
		$sqltemp  = "select cc.id as cid,su.id as sid,su.user as users,if(su.stop=1,'停用','启用') as stopname,'密' as pwd,ca.name as area,cc.*,FROM_UNIXTIME(cc.start_time,'%Y-%m-%d %H:%i:%S') as st_time ";
		$sqltemp .= "from sys_user as su,crm_customer as cc left join crm_area as ca on cc.area_id = ca.id ";
		$sqltemp .= "where su.ugroup = 1 and cc.login = su.id  ";
		$sql = "select * from ($sqltemp)a where 1=1 ";
		if($p["user"] != ""){$sql .= " and users like '%".$p["user"]."%'";}
		if($p["name"] != ""){$sql .= " and name like '%".$p["name"]."%'";}
		if($p["area"] != ""){$sql .= " and area_id in (".$p["area"].")";}
		if($p["types"] != ""){$sql .= " and types = '".$p["types"]."'";}
		$sql .= " order by ".$p["sortField"]." ".$p["sortOrder"];
		$sql .= " limit [start],[end]";
		//echo $sql;
		return $this->sql($sql,array("start"=>$p["r"]*$p["p"],"end"=>$p["r"]))->s();
	}
	
	// 顾客(详情)
	function getCustomerInfo($p)
	{
		return $this->sql("select cc.*,if(ca.name is null,cc.adders,concat(cc.adders,'(',ca.name,')')) as area,if(cc.types=0,'潜在客户','正在客户') as types from crm_customer as cc left join crm_area as ca on cc.area_id = ca.id where cc.id=[id] ",$p)->s(1);
	}
	
	// 顾客同步
	function sny($p){
		$sql = "select su.user as users,su.password as pwd,cc.email as email ";
		$sql.= "from sys_user as su,crm_customer as cc ";
		$sql.= "where su.id=cc.login and su.id=[sid]";
		return $this->sql($sql,$p)->s(1);
	}
	
	// 顾客同步
	function setsny($p){
		$sql = "update crm_customer set synchronous='已同步' where login=[sid]";
		return $this->sql($sql,$p)->u();
	}
	
	// 顾客(删除)
	function CustomerDel($p)
	{
		$this->sql("delete from sys_user where id in (select login from crm_customer where id = [id])",$p)->d();
		$this->sql("delete from crm_customer where id = [id]",$p)->d();
	}
	
	// 顾客(修改)
	function CustomerUpd($p){

		$this->sql("update crm_customer set `level`='[level]',`remark`='[remark]' where `id`=[cid] ",$p)->u();
	}

    // 顾客(修改)
    function ICustomerUpd($data, $id)
    {
        $sql = updateSql('crm_customer', $data, 'id='.$id);//echo $sql;exit;
        $this->sql($sql)->u();
    }
	
	// 顾客（停用）
	function StopStatus($p){
		$this->sql("update sys_user set `stop`=[stop] where `id`=[id] ",$p)->u();
	}
	
	// 顾客（重置密码）
	function PwdReload($p){
		$this->sql("update sys_user set `password`=md5('888888') where `id`=[id] ",$p)->u();
	}
	
	// 顾客（变更类型）
	function TypesUp($p){
		$this->sql("update crm_customer set `types`=[types] where `id`=[id] ",$p)->u();
	}
	
	// 顾客(详情)
	function getCustomerByName($p)
	{
		return $this->sql("select * from crm_customer where name='[name]' ",$p)->s(1);
	}
	
	function setUsersAdd($p){
		return $this->sql("insert into sys_user(`ugroup`,`user`,`password`)values(1,'[user]','888888')",$p)->i();
	}
	
	function setCustomerAdd($p){
		return $this->sql("insert into crm_customer(`types`,`login`,`name`,`mobile_phone`,`tel`,`email`)values(0,[login],'[name]','[mobile_phone]','[tel]','[email]')",$p)->i();
	}
	
	function getCustomerByPhone($id){
		$re = $this->sql("select mobile_phone from crm_customer where id=$id ")->s(1);
		if($re){
			return $re['mobile_phone'];
		}else{
			return '';
		}
	}

    // 审核客户
    function verifyCustomer($p)
    {
        $up = $p['type'] == 1 ? '已通过' : '未通过';
        $this->sql("update crm_customer set `verify`='".$up."' where `id`=".$p['id'])->u();
    }
}